package com.carrental.api.oms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.carrental.api.oms.domain.vo.OmsOrderVo;
import com.carrental.common.response.CommonPage;
import com.carrental.common.response.CommonResult;
import com.carrental.api.common.query.CommonQueryParam;
import com.carrental.api.oms.domain.OmsOrder;
import com.carrental.api.oms.domain.bo.OmsLendCheckPost;
import com.carrental.api.oms.domain.bo.OmsOrderBo;
import com.carrental.api.oms.domain.bo.OmsReturnCheckPost;
import com.carrental.api.oms.domain.vo.OmsOrderDetailVo;
import com.carrental.api.oms.service.IOmsOrderService;
import com.carrental.common.annotation.Log;
import com.carrental.common.core.controller.BaseController;
import com.carrental.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单信息Controller
 *
 * @author liangyi
 * @date 2022-03-20
 */
@Validated
@Api(value = "订单信息控制器", tags = {"订单信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/oms/order")
public class OmsOrderController extends BaseController {
    @Autowired
    private  IOmsOrderService orderService;
    /**
     * 导出订单信息列表
     */
    @ApiOperation("导出订单信息列表")
    @SaCheckPermission("oms:order:export")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated OmsOrderBo bo, HttpServletResponse response) {
//        List<OmsOrderVo> list = orderService.queryList(bo);
//        ExcelUtil.exportExcel(list, "订单信息", OmsOrderVo.class, response);
    }

    @PostMapping()
    @ApiOperation("生成订单")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> orderCreate(@RequestBody OmsOrderBo omsOrderPost) throws Exception {
        return CommonResult.success(orderService.create(omsOrderPost));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除订单信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> orderRemove(@ApiParam(name = "id", value = "订单标识", required = true)
                                             @PathVariable Long id) {
        return CommonResult.success(orderService.delete(id));
    }

    @PutMapping("/{id}")
    @ApiOperation("修改订单信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> orderUpdate(@ApiParam(name = "id", value = "订单标识", required = true)
                                             @PathVariable Long id, @RequestBody OmsOrderBo order) {
        return CommonResult.success(orderService.update(id,order));
    }

    @PutMapping("/lend/{id}")
    @ApiOperation("出车！")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> orderLend(@ApiParam(name = "id", value = "订单标识", required = true)
                                           @PathVariable Long id, @RequestBody OmsLendCheckPost lendCheckPost) {
        return CommonResult.success(orderService.lendCheckOrder(id,lendCheckPost));
    }

    @PutMapping("/return/{id}")
    @ApiOperation("还车！")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> orderReturn(@ApiParam(name = "id", value = "订单标识", required = true)
                                             @PathVariable Long id,@RequestBody OmsReturnCheckPost omsReturnCheckPost) {
        return CommonResult.success(orderService.returnCheckOrder(id,omsReturnCheckPost));
    }

    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> orderCancel(@ApiParam(name = "id", value = "订单标识", required = true)
                                             @PathVariable Long id) {
        return CommonResult.success(orderService.cancel(id));
    }

    @GetMapping()
    @ApiOperation("获取订单列表")
    public CommonResult<List<OmsOrder>> orderList(CommonQueryParam commonQueryParam) {
        commonQueryParam.setSize((int) orderService.count());
        return CommonResult.success(orderService.list(commonQueryParam));
    }

    @GetMapping("/myorder")
    @ApiOperation("获取我的全部订单")
    public CommonResult<List<OmsOrderVo>> myOrderList(CommonQueryParam commonQueryParam) {
        commonQueryParam.setSize((int) orderService.count());
        return CommonResult.success(orderService.myOrderList(commonQueryParam));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取订单详情")
    public CommonResult<OmsOrder> orderObj(@PathVariable Long id) {
        return CommonResult.success(orderService.detail(id));
    }

    @GetMapping("/all-detail/{id}")
    @ApiOperation("获取全套订单信息详情")
    public CommonResult<OmsOrderDetailVo> allOrderDetail(@PathVariable Long id) {
        return CommonResult.success(orderService.allDetail(id));
    }

    @GetMapping("/page")
    @ApiOperation("获取订单分页")
    public CommonResult<CommonPage<OmsOrder>> orderPage(CommonQueryParam commonQueryParam) {
        return CommonResult.success(CommonPage.restPage(orderService.pageList(commonQueryParam)));
    }

    @GetMapping("/page/by-store/{id}")
    @ApiOperation("根据门店id获取其订单分页")
    public CommonResult<CommonPage<OmsOrder>> orderPage(@PathVariable Long id, CommonQueryParam commonQueryParam) {
        return CommonResult.success(CommonPage.restPage(orderService.pageListByStoreId(id,commonQueryParam)));
    }
}
